package com.shelpe.serivces.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;

import com.shelpe.serivces.conf.git.CachableMultipleJGitEnvironmentRepository;

@EnableCaching
@Configuration
public class AppConfiguration extends CachingConfigurerSupport{

	@Autowired
	private ConfigurableEnvironment environment;

	@Autowired
	private ConfigServerProperties server;

	@Bean
	public EnvironmentRepository environmentRepository() {
		CachableMultipleJGitEnvironmentRepository repository = new CachableMultipleJGitEnvironmentRepository(this.environment);
		if (this.server.getDefaultLabel()!=null) {
			repository.setDefaultLabel(this.server.getDefaultLabel());
		}
		return repository;
	}
	@Bean
	public CacheManager cacheManager(){
		return new ConcurrentMapCacheManager();
	}
	@Bean
	public KeyGenerator keyGenerator(){
		return new SimpleKeyGenerator();
	}
}
